package com.laiketui.plugin.common.mapper;

import com.laiketui.plugin.common.domain.base.seckill.SecondsActivityModel;
import com.laiketui.plugin.common.tool.db.BaseMapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 秒杀活动/商品表
 *
 * @author Trick
 * @date 2021/11/17 14:40
 */
public interface SecondsActivityModelMapper extends BaseMapper<SecondsActivityModel> {

    /**
     * 增减库存
     *
     * @param acId -
     * @param num  -
     * @return int
     * @author Trick
     * @date 2021/4/12 15:09
     */
    @Update("update lkt_seconds_activity set num = num + #{num} where id=#{acId} and is_delete  = 0")
    int addStockNum(int acId, int num);


    //状态修改---已结束
    @Update("update lkt_seconds_activity a set status=3,isshow=0 where a.endtime <= #{sysDate} and is_delete=0")
    int secondsEndStatus(Date sysDate);

    //状态修改---未开始
    @Update("update lkt_seconds_activity a set status=1 where a.starttime > #{sysDate} and a.endtime > #{sysDate} and is_delete=0 ")
    int secondsNotOpenStatus(Date sysDate);

    //状态修改---进行中
    @Update("update lkt_seconds_activity a set status=2 where a.starttime < #{sysDate} and a.endtime > #{sysDate} and status!=4 and is_delete=0 ")
    int secondsOpenStatus(Date sysDate);

    //状态修改---已抢光
    @Update("update lkt_seconds_activity a set status=5 where a.num<1 and is_delete=0 ")
    int secondsTootAllStatus();

    //状态修改---预告
    @Update("update lkt_seconds_activity a,lkt_seconds_label b set status=4 where a.label_id=b.label_id and b.recovery=0 and b.mch_id={mchId} " +
            " and a.starttime <=#{heraldDate} and a.starttime > #{sysDate} and a.endtime > #{sysDate} and is_delete=0")
    int secondsHeraldStatus(Integer mchId, Date heraldDate, Date sysDate);

    /**
     * 获取秒杀预告
     *
     * @param heraldDate - 预告时间
     * @param sysDate    - 系统时间
     * @return Map
     * @author Trick
     * @date 2021/4/13 17:18
     */
    @Select("SELECT a.*,c.product_title FROM lkt_seconds_activity a LEFT JOIN lkt_product_list c ON a.goodsId = c.id " +
            " inner join lkt_seconds_label lab on a.label_id=lab.label_id and lab.recovery=0 and lab.mch_id={mchId} " +
            " where a.starttime <= #{heraldDate} " +
            " and a.starttime < #{sysDate} " +
            " and a.endtime > #{sysDate} " +
            " and a.isNotice = 0 ")
    List<Map<String, Object>> getSecondsByHerald(Integer mchId, Date heraldDate, Date sysDate);

    /**
     * 获取商品规格信息-后台添加商品规格弹窗
     *
     * @param goodsId -
     * @param acId    -
     * @return List
     * @author Trick
     * @date 2021/11/17 19:59
     */
    @Select(" select a.id,a.pid goodsId,a.attribute,a.num,a.price,IFNULL(c.num,0) secNum,IFNULL(c.max_num,0) secMaxNum from lkt_configure a " +
            " left join lkt_seconds_activity b on b.id = #{acId} and b.is_delete=0 " +
            " left JOIN lkt_seconds_pro c on b.id=c.activity_id and c.is_delete=0 and c.attr_id=a.id and c.num>0 " +
            " where a.recycle=0 " +
            " and a.pid=#{goodsId} ")
    List<Map<String, Object>> getSecondsAttrList(int goodsId, int acId);


    /**
     * 获取秒杀活动所属店铺
     *
     * @param acId -
     * @return Integer
     * @author Trick
     * @date 2022/1/17 19:38
     */
    @Select("select distinct b.mch_id from lkt_seconds_activity a,lkt_seconds_label b where a.id =#{acId} and a.label_id=b.id ")
    Integer getSecMchId(Integer acId);
}